﻿using Engine;
using Game;
namespace Mekiasm
{
    public class PortableTeleporter : ItemFlat
    {
        public static int Index;
        public bool SetOne = false;
        public Point3 Start = Point3.Zero;
        public PortableTeleporter() : base("便携传送器", MekiasmInit.GetFlatTexture(MekiasmInit.R2, "便携传送器"))
        {
            
        }
        public override bool OnUse(Ray3 aim, ComponentMiner componentMiner, TerrainRaycastResult? raycast)
        {
            if (raycast.HasValue)
            {
                int v = ILibrary.SubsystemTerrain.Terrain.GetCellValueFast(raycast.Value.CellFace.X, raycast.Value.CellFace.Y, raycast.Value.CellFace.Z);
                if (ILibrary.GetBasicValue(v) == TransmitMachine.Index && TransmitMachine.saves.TryGetValue(raycast.Value.CellFace.Point, out MutiBlockCheck.TranmitResult tranmitResult))
                {
                    if (!SetOne)
                    {
                        DisplayMsg(componentMiner.ComponentPlayer, "设置传送点1成功，请设置传送点2");
                        Start = raycast.Value.CellFace.Point;
                        SetOne = true;
                    }
                    else
                    {
                        if (Start == raycast.Value.CellFace.Point)
                        {
                            DisplayMsg(componentMiner.ComponentPlayer, "已取消传送点1设置");
                        }
                        else if (TransmitMachine.saves.TryGetValue(Start, out MutiBlockCheck.TranmitResult ctranmitResult))
                        {
                            tranmitResult.Connection = Start;
                            tranmitResult.Connected = true;
                            ctranmitResult.Connected = true;
                            ctranmitResult.Connection = raycast.Value.CellFace.Point;
                            //替换成传送门方块
                            TransmitMachine.SetInsideBlocks(tranmitResult.insidePoints, TransmitItem.Index);
                            //替换成传送门方块
                            TransmitMachine.SetInsideBlocks(ctranmitResult.insidePoints, TransmitItem.Index);
                            DisplayMsg(componentMiner.ComponentPlayer, "设置传送点2成功，传送门已连接");
                        }
                        else
                        {
                            DisplayMsg(componentMiner.ComponentPlayer, "传送点1被摧毁，传送门连接失败");
                        }
                        Start = Point3.Zero;
                        SetOne = false;
                    }
                }
            }
            return true;
        }

    }
}
